﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMS.IDAL;
using System.Data.SqlClient;
using System.Data;
using WMS.DBUtility;

namespace WMS.SQLServerDAL
{
    public class StockMngrData : IStockMngr
    {
        private readonly string sp_WHSGetAllBySupIdOrNo = "WHSGetAllBySupIdOrNo";
        private readonly string sp_WHSAddModel = "WHSAddModel";
        private readonly string sp_WHSModifyByModel = "WHSModifyByModel";
        private readonly string sp_WHSDelWhsByIds = "WHSDelWhsByIds";

        public int AddStock(Model.WhsModel model)
        {
            SqlParameter[] parameters = {
				new SqlParameter("@supId", SqlDbType.VarChar, 100),
				new SqlParameter("@whsNo", SqlDbType.VarChar, 100),
				new SqlParameter("@whsDesc", SqlDbType.VarChar, 200),
                new SqlParameter("@user", SqlDbType.VarChar, 100)
			};
            parameters[0].Value = model.SupId;
            parameters[1].Value = model.WhsWhs;
            parameters[2].Value = model.WhsDesc;
            parameters[3].Value = model.CtMan;
       
            try
            {
                int result = DbHelperSQL.ExecuteStoredProcNonQuery(sp_WHSAddModel, parameters);

                return result ;
            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        public int ModifyStock(Model.WhsModel model)
        {
            SqlParameter[] parameters = {
				new SqlParameter("@whsNo", SqlDbType.VarChar, 100),
				new SqlParameter("@whsDesc", SqlDbType.VarChar, 200),
                new SqlParameter("@user", SqlDbType.VarChar, 100)
			};
            parameters[0].Value = model.WhsWhs;
            parameters[1].Value = model.WhsDesc;
            parameters[2].Value = model.CtMan;

            try
            {
                int result = DbHelperSQL.ExecuteStoredProcNonQuery(sp_WHSModifyByModel, parameters);

                return result;
            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        public System.Data.DataTable GetStockInfo(Model.WhsModel model)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@supId", SqlDbType.VarChar, 100),
				new SqlParameter("@whsNo", SqlDbType.VarChar, 100)
            
			};
            parameters[0].Value = model.SupId;
            parameters[1].Value = model.WhsWhs;

           DataTable dt = DbHelperSQL.ExecuteStoredProcDataSet(sp_WHSGetAllBySupIdOrNo, parameters).Tables[0];

           return dt; 
        }

        public int DelStockByIds(string ids)
        {
            SqlParameter[] parameters = {
				new SqlParameter("@whsNos", SqlDbType.VarChar, 200)};
            parameters[0].Value = ids;
            try
            {
                int result = DbHelperSQL.ExecuteStoredProcNonQuery(sp_WHSDelWhsByIds, parameters);

                return result;
            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        public DataTable RoleGetAllData(string supId)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@supId", SqlDbType.VarChar, 100)
            
			};
            parameters[0].Value = supId;

            DataTable dt = DbHelperSQL.ExecuteStoredProcDataSet("OrderOnlineRoleGetAll", parameters).Tables[0];

            return dt; 
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 设置仓库权限
        /// </summary>
        /// <param name="role"></param>
        /// <param name="whss"></param>
        /// <param name="uName"></param>
        /// <param name="supId"></param>
        /// <returns></returns>
        public int SetWhsPermissData(WMS.Model.OrderOnline_Role role, string whss, string uName, string supId)
        {
            SqlParameter[] parameters = {
				new SqlParameter("@RoleName", SqlDbType.VarChar, 200),
                new SqlParameter("@WhsWhs", SqlDbType.VarChar, 200),
                new SqlParameter("@userName", SqlDbType.VarChar, 200),                      
                new SqlParameter("@supId", SqlDbType.VarChar, 200),
                  new SqlParameter("@RoleId", SqlDbType.VarChar, 200)};

            parameters[0].Value = role.RoleName;
            parameters[1].Value = whss;
            parameters[2].Value = uName;
            parameters[3].Value = supId;
            parameters[4].Value = role.Id;
            try
            {
                int result = DbHelperSQL.ExecuteStoredProcNonQuery("WpeSetRoles", parameters);

                return result;
            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message, ex);
            }
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 获取角色对于的仓库信息
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="supId"></param>
        /// <returns></returns>
        public DataTable GetWhsRoleByRoleIdData(string roleId, string supId)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@supId", SqlDbType.VarChar, 100),
                new SqlParameter("@roleId", SqlDbType.VarChar, 100)
			};
            parameters[0].Value = supId;
            parameters[1].Value = roleId;
       
            DataTable dt = DbHelperSQL.ExecuteStoredProcDataSet("WhsRoleGetByRoleID", parameters).Tables[0];

            return dt; 
        }
    }
}
